package com.runtime.hongyunmanage.utils;

import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class UploadUtils {

    public static void saveFileToJarPath(Class<?> cls, String fileName, MultipartFile file) throws IOException {
        // 获取 JAR 包所在目录
        String uploadDir = System.getProperty("user.dir") + "/image/";
        System.out.println("Upload directory: " + uploadDir);

        Path uploadPath = Paths.get(uploadDir);
        if (!Files.exists(uploadPath)) {
            Files.createDirectories(uploadPath);
        }

        Path path = uploadPath.resolve(fileName);
        try (InputStream in = file.getInputStream()) {
            Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
        }
    }
}